import { FC } from 'react';
import { List, Button, Space, Tag } from 'antd';
import { DownloadOutlined, DeleteOutlined, EyeOutlined } from '@ant-design/icons';
import { IndustryReportAttachment } from 'types/industry-report';
import styles from './AttachmentList.module.css';

interface AttachmentListProps {
  attachments: IndustryReportAttachment[];
  onPreview?: (file: IndustryReportAttachment) => void;
  onDelete?: (file: IndustryReportAttachment) => void;
}

const AttachmentList: FC<AttachmentListProps> = ({
  attachments,
  onPreview,
  onDelete
}) => {
  // 格式化文件大小
  const formatFileSize = (bytes: number) => {
    if (bytes === 0) return '0 B';
    const k = 1024;
    const sizes = ['B', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
  };

  // 获取文件类型标签的颜色
  const getTypeColor = (type: string) => {
    const typeMap: Record<string, string> = {
      'pdf': 'red',
      'doc': 'blue',
      'docx': 'blue',
      'xls': 'green',
      'xlsx': 'green',
      'jpg': 'purple',
      'png': 'purple'
    };
    return typeMap[type.toLowerCase()] || 'default';
  };

  return (
    <List
      className={styles.list}
      itemLayout="horizontal"
      dataSource={attachments}
      renderItem={file => (
        <List.Item
          actions={[
            onPreview &&<Button
              type="link"
              icon={<EyeOutlined />}
              onClick={() => onPreview?.(file)}
            >
              预览
            </Button>,
            <Button
              type="link"
              icon={<DownloadOutlined />}
              onClick={() => window.open(file.url)}
            >
              下载
            </Button>,
            onDelete && <Button
              type="link"
              danger
              icon={<DeleteOutlined />}
              onClick={() => onDelete?.(file)}
            >
              删除
            </Button>
          ]}
        >
          <List.Item.Meta
            title={file.fileName}
            description={
              <Space>
                <Tag color={getTypeColor(file.fileType)}>{file.fileType.toUpperCase()}</Tag>
                <span>{formatFileSize(file.fileSize)}</span>
              </Space>
            }
          />
        </List.Item>
      )}
    />
  );
};

export default AttachmentList;